// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// ---
// Author: Jeffrey Junfeng Pan (panjunfeng@gmail.com)

// access transactions that stored in memory as vectors.

#ifndef FP_GROWTH_TRANSACTIONS_MEMORY_IMPL_H_
#define FP_GROWTH_TRANSACTIONS_MEMORY_IMPL_H_

#include <vector>

#include "transactions.h"

namespace frequent_pattern_mining {

// ------------------------------------------------------------
//                      Declaration
// ------------------------------------------------------------

template <typename T>
class TransactionsMemoryImpl : public Transactions<T> {
 public:
  // Constructor.
  TransactionsMemoryImpl();

  // Use external transaction vectors for initialization but not own the
  // external data. The frequency of each transaction is 1 by default.
  explicit TransactionsMemoryImpl(vector<vector<T> >* transactions);

  // Use external transaction vectors for initialization but not own the
  // external data. The frequencies are also externally given and not owned.
  TransactionsMemoryImpl(vector<vector<T> >* transactions,
                         vector<int>* frequencies);

  // Destructor.
  ~TransactionsMemoryImpl();

  // Return the size of transactions.
  int size();

  // If there is no transaction at all.
  bool empty();

  // Return the transaction given the index.
  // The implementation shall optimize for sequential reading.
  vector<T>& operator[](int index);

  // Return the frequency given the index.
  int operator()(int index);

  // Whether or not the transaction exists given the index.
  bool is_valid(int index);

  // Insert a new transaction with default frequency = 1.
  void push_back(const vector<T>& transaction);

  // Insert a new transaction with a given frequency.
  void push_back(const vector<T>& transaction, int frequency);

 private:

  // The pointer to the transactions. May or may not own the data.
  vector<vector<T> >* transactions_;

  // Whether own the transactions or not.
  bool own_transactions_;

  // The pointer to the frequencies. May or may not own the data.
  vector<int>* frequencies_;

  // Whether own the frequencies or not.
  bool own_frequencies_;

  DISALLOW_EVIL_CONSTRUCTORS(TransactionsMemoryImpl);
};

// ------------------------------------------------------------
//                      Definition
// ------------------------------------------------------------

// Constructor.
template <typename T>
TransactionsMemoryImpl<T>::TransactionsMemoryImpl()
  : transactions_(new vector<vector<T> >),
    own_transactions_(true),
    frequencies_(new vector<int>),
    own_frequencies_(true) {
}

// Use external transaction vectors for initialization but not own the
// external data. The frequency of each transaction is 1 by default.
template <typename T>
TransactionsMemoryImpl<T>::TransactionsMemoryImpl(
    vector<vector<T> >* transactions) : transactions_(transactions),
                                        own_transactions_(false),
                                        frequencies_(new vector<int>),
                                        own_frequencies_(true) {
  frequencies_->resize(transactions_->size());
  for (int i = 0; i < frequencies_->size(); ++i) {
    (*frequencies_)[i] = 1;
  }
}

// Use external transaction vectors for initialization but not own the
// external data. The frequencies are also externally given and not owned.
template <typename T>
TransactionsMemoryImpl<T>::TransactionsMemoryImpl(
    vector<vector<T> >* transactions,
    vector<int>* frequencies) : transactions_(transactions),
                                own_transactions_(false),
                                frequencies_(frequencies),
                                own_frequencies_(false) {
  CHECK_EQ(transactions_->size(), frequencies_->size())
    << ": size mismatches";
}

// Destructor.
template <typename T>
TransactionsMemoryImpl<T>::~TransactionsMemoryImpl() {
  if (own_transactions_) {
    delete transactions_;
  }
  if (own_frequencies_) {
    delete frequencies_;
  }
}

// Return the size of transactions.
template <typename T>
int TransactionsMemoryImpl<T>::size() {
  return transactions_->size();
}

// If there is no transaction at all.
template <typename T>
bool TransactionsMemoryImpl<T>::empty() {
  return (transactions_->size() == 0);
}

// Return the transaction given the index.
// The implementation shall optimize for sequential reading.
template <typename T>
vector<T>& TransactionsMemoryImpl<T>::operator[](int index) {
  CHECK_GE(index, 0) << ": index must be >= 0";
  CHECK_LT(index, transactions_->size()) << ": index must be < transaction num";
  return (*transactions_)[index];
}

// Return the frequency given the index.
template <typename T>
int TransactionsMemoryImpl<T>::operator()(int index) {
  CHECK_GE(index, 0) << ": index must be >= 0";
  CHECK_LT(index, frequencies_->size()) << ": index must be < transaction num";
  return (*frequencies_)[index];
}

// Whether or not the transaction exists given the index.
template <typename T>
bool TransactionsMemoryImpl<T>::is_valid(int index) {
  if (index >= 0 && index < transactions_->size()) {
    return true;
  } else {
    return false;
  }
}

// Insert a new transaction with default frequency = 1.
template <typename T>
void TransactionsMemoryImpl<T>::push_back(const vector<T>& transaction) {
  transactions_->push_back(transaction);
  frequencies_->push_back(1);
}

// Insert a new transaction with a given frequency.
template <typename T>
void TransactionsMemoryImpl<T>::push_back(const vector<T>& transaction,
                                          int frequency) {
  transactions_->push_back(transaction);
  frequencies_->push_back(frequency);
}
}  // namespace frequent_pattern_mining

#endif  // FP_GROWTH_TRANSACTIONS_MEMORY_IMPL_H_
